package com.jxnu.escms.Control.UserControl;

import com.jxnu.escms.Pojo.User;
import com.jxnu.escms.Service.UserService.EmailService;
import com.jxnu.escms.Service.UserService.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yihui
 * 邮箱验证
 */
@Slf4j
@RestController
@RequestMapping("/retrieve")
public class EmailAut {

    @Resource
    private UserService userService;

    @Resource
    private EmailService emailService;

    /**
     * 传入用户名，邮箱，
     * 判断是否存在用户。
     */
    @RequestMapping("/{name}/{email}")
    public Map emailAut(@PathVariable(value = "name", required = false) String name,
                        @PathVariable(value = "email", required = false) String email,
                        HttpSession session){
        Map<String,Object> map = new HashMap<>();
        // 首先，判断是否存在此用户
        User user1 = userService.containUserByNameAndEmail(name, email);
        if(user1 != null){
            map.put("result", "success");
            // 发送邮箱
            String verCode = emailService.sendEmail(email, name);
            map.put("verCode", verCode);
        }
        else{
            // 若找不到，则说明用户不存在
            map.put("result", "worry");
            map.put("message", "用户不存在");
        }
        return map;
    }


    @PostMapping("/changePass")
    public Map changePass(@RequestBody Map<String, String> data){
        String name = data.get("name");
        String email = data.get("email");
        String password = data.get("password");
        int i = userService.changePassword(name, email, password);
        Map<String,String> res = new HashMap<>();
        if(i == 1){
            res.put("change", "success");
        }
        return res;
    }
}
